package com.sk.weichat.ui.me.wallet;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.pay.sk.AlipayAuth;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.me.wallet.view.AliPayView;
import com.sk.weichat.ui.me.wallet.view.AuthCallbackView;
import com.sk.weichat.ui.me.wallet.viewmode.AliPayViewMode;
import com.sk.weichat.util.LiveDataBus;
import com.sk.weichat.util.ToastUtil;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AlipayAuthActivity extends BaseActivity implements AliPayView, AuthCallbackView {

    AliPayViewMode aliPayViewMode;

    private final static String alipayNameKey = "alipayNameKey";

    private String event = "";
    TextView tvAlipayName;

    public static void startAlipayAuthActivity(String alipayName, String event, Context context) {
        Intent intent = new Intent(context, AlipayAuthActivity.class);
        intent.putExtra(alipayNameKey, alipayName);
        intent.putExtra("event", event);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay_auth);
        event = getIntent() != null ? getIntent().getStringExtra("event") : "";
        String alipayname = getIntent() != null ? getIntent().getStringExtra(alipayNameKey) : "";
        SpannableString spannableString = new SpannableString("支付宝: " + alipayname);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.color_27A2FF));
        spannableString.setSpan(foregroundColorSpan, 4, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvAlipayName = findViewById(R.id.tv_alipay_name);
        tvAlipayName.setText(spannableString);
        TextView tvTitle = findViewById(R.id.tv_title_center);
        tvTitle.setText("身份验证");
        findViewById(R.id.tv_verify).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //支付宝授权
                aliPayViewMode.getAlipayAutoMsg();
            }
        });
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        aliPayViewMode = new AliPayViewMode(coreManager, this);
    }

    @Override
    public void AlipayAuthMsgResult(ObjectResult<String> result) {
        if (result.getResultCode() == 1) {
            AlipayAuth alipayAuth = new AlipayAuth(this);
            alipayAuth.callAuth(this, result.getData(), false);
        } else {
            ToastUtil.showToast(this, result.getResultMsg());
        }
    }

    @Override
    public void AlipayAutoCodeResult(ObjectResult<String> result) {

    }

    @Override
    public void AlipayUnbindResult(ObjectResult<String> result) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showLoadFailMsg(String err) {

    }

    @Override
    public void AlipayAuthCallbackResult(String authCode) {
        if (TextUtils.isEmpty(authCode)) {
            showLoadFailMsg(getString(R.string.alipay_auth_cancle));
        } else {
            compareAlipay(authCode);
        }
    }

    public void compareAlipay(String code) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("code", code);
        HttpUtils.post().url(coreManager.getConfig().ALIPAY_COMPARE)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {
                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        //
                        if (result.getResultCode() == 1) {
                            boolean data;
                            try {
                                data = Boolean.valueOf(result.getData());
                                if (data) {
                                    LiveDataBus.get().with(event).postValue("");
                                    tvAlipayName.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            setResult(Activity.RESULT_OK);
                                            finish();
                                        }
                                    }, 200);
                                } else {
                                    ToastUtil.showToast(mContext, getString(R.string.tips_alipay_auth_failed));
                                }
                            } catch (Exception e) {

                            }

                        } else {
                            ToastUtil.showToast(mContext, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {

                    }
                });
    }
}
